package com.ym.pms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.BuildingMapper;
import com.ym.pms.dao.ItemMapper;
import com.ym.pms.getCoding.NewCoding;
import com.ym.pms.model.Item;
import com.ym.pms.service.BuildingService;
import com.ym.pms.service.ItemService;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    ItemMapper dao;
    @Autowired
    BuildingService buSercice;
    @Autowired
    NewCoding newCoding;

    @Override
    public PageInfo<Item> findAllItems(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Item> items = dao.queryItemList(false);
        if(items.size()>0){
            PageInfo<Item> pageInfo = new PageInfo<Item>(items);
            return pageInfo;
        }else{
            return null;
        }
    }

    @Override
    public PageInfo<Item> findAllItemsOfS(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Item> items = dao.queryItemList(true);
        if(items.size()>0){
            PageInfo<Item> pageInfo = new PageInfo<Item>(items);
            return pageInfo;
        }else{
            return null;
        }
    }

    @Override
    public Item findById(String id) {
        Item item = dao.selectByPrimaryKey(id);
        if(item == null) {
            return null;
        }else{
            return item;
        }
    }

    @Override
    public int update(Item item) {
        if(item.getItId() == null || item.getItId().equals("")) {
            return -1;
        }else if(dao.selectByPrimaryKey(item.getItId()) == null){
            return -2;
        }else {
            return dao.updateByPrimaryKeySelective(item);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int bulkUpdate(List<Item> items) {
        int a = 0;
        for (Item item : items) {
            a += dao.updateByPrimaryKey(item);
        }
        return a;
    }

    @Override
    public int deleteById(String id) {
        buSercice.deleteAllByItId(id);
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int addItem(Item item) {
        String coding = this.getItemCoding();
        item.setItId(coding);
        return dao.insertSelective(item);
    }

    @Override
    public Integer getItemMaxNo() {
        Integer itemMaxNo = dao.getItemMaxNo();
        if (itemMaxNo == null){
            return 0;
        }else{
            return itemMaxNo;
        }
    }

    @Override
    public String getItemCoding() {
        Integer maxNo = this.getItemMaxNo();
        maxNo += 1;
        String it_id = newCoding.getCoding("IT", maxNo);
        return it_id;
    }

    @Override
    public PageInfo<Item> fuzzyQuery(Item item,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Item> allSelective = dao.findAllSelective(item,false);
        PageInfo<Item> pageInfo = new PageInfo<Item>(allSelective);
        return allSelective.size() > 0 ? pageInfo : null;
    }

    @Override
    public PageInfo<Item> fuzzyQueryOfS(Item item, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Item> allSelective = dao.findAllSelective(item,true);
        PageInfo<Item> pageInfo = new PageInfo<Item>(allSelective);
        return allSelective.size() > 0 ? pageInfo : null;
    }

    @Override
    public Integer getTotalNumber() {
        return dao.getTotalNumber();
    }
}
